package com.demo.springsecuritystudy.handle;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class SecurityAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private String url;

    public SecurityAuthenticationSuccessHandler(String url) {
        this.url = url;
    }

    /**
     * 前后端分离进行重定向
     * @param httpServletRequest
     * @param httpServletResponse
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Authentication authentication) throws IOException, ServletException {
        log.info("request ip address is {}", httpServletRequest.getRemoteAddr());
        httpServletResponse.sendRedirect(url);
    }
}
